package com.qth.valet.alipay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.domain.ExtUserInfo;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.domain.GoodsDetail;

import com.alipay.api.FileItem;

import java.util.*;

public class AlipayTradeAppPay {

    /*public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();

        // 设置商户订单号
        model.setOutTradeNo("202509011234567890");
        // 设置订单总金额
        model.setTotalAmount("0.01");
        // 设置订单标题
        model.setSubject("测试");

        request.setBizModel(model);

        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        String orderStr = response.getBody();
        System.err.println(orderStr);
        String[] kvs = orderStr.split("&");
        Map<String, String> map = new HashMap<>();
        for (String kv : kvs) {
            String[] k_v = kv.split("=");
            map.put(k_v[0], k_v[1]);
        }
        System.err.println(JSON.toJSONString(map));

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }*/

    /*public static void main(String[] args) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();

        // 设置商户订单号
        model.setOutTradeNo("70501111111S001111119");

        // 设置订单总金额
        model.setTotalAmount("9.00");

        // 设置订单标题
        model.setSubject("大乐透");

        // 设置产品码
        model.setProductCode("QUICK_MSECURITY_PAY");

        // 设置订单包含的商品列表信息
        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
        GoodsDetail goodsDetail0 = new GoodsDetail();
        goodsDetail0.setGoodsName("ipad");
        goodsDetail0.setAlipayGoodsId("20010001");
        goodsDetail0.setQuantity(1L);
        goodsDetail0.setPrice("2000");
        goodsDetail0.setGoodsId("apple-01");
        goodsDetail0.setGoodsCategory("34543238");
        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
        goodsDetail.add(goodsDetail0);
        model.setGoodsDetail(goodsDetail);

        // 设置订单绝对超时时间
        model.setTimeExpire("2016-12-31 10:05:00");

        // 设置业务扩展参数
        ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        extendParams.setHbFqSellerPercent("100");
        extendParams.setHbFqNum("3");
        extendParams.setIndustryRefluxInfo("{\"scene_code\":\"metro_tradeorder\",\"channel\":\"xxxx\",\"scene_data\":{\"asset_name\":\"ALIPAY\"}}");
        extendParams.setRoyaltyFreeze("true");
        extendParams.setCardType("S0JP0000");
        model.setExtendParams(extendParams);

        // 设置公用回传参数
        model.setPassbackParams("merchantBizType%3d3C%26merchantBizNo%3d2016010101111");

        // 设置商户的原始订单号
        model.setMerchantOrderNo("20161008001");

        // 设置外部指定买家
        ExtUserInfo extUserInfo = new ExtUserInfo();
        extUserInfo.setCertType("IDENTITY_CARD");
        extUserInfo.setCertNo("362334768769238881");
        extUserInfo.setName("李明");
        extUserInfo.setMobile("16587658765");
        extUserInfo.setMinAge("18");
        extUserInfo.setNeedCheckInfo("F");
        extUserInfo.setIdentityHash("27bfcd1dee4f22c8fe8a2374af9b660419d1361b1c207e9b41a754a113f38fcc");
        model.setExtUserInfo(extUserInfo);

        // 设置通知参数选项
        List<String> queryOptions = new ArrayList<String>();
        queryOptions.add("hyb_amount");
        queryOptions.add("enterprise_pay_info");
        model.setQueryOptions(queryOptions);

        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
        String orderStr = response.getBody();
        System.out.println(orderStr);

        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }*/

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCDZTTpaqLqfP1zUa71YKX60zl0CVFSZ1NENrnBJxr3R2dFPgIdIKAPYEFwBLUvl4gPA72HXOAMXNlrAwLvaIWUI6fCc+hQo36wL06Nle2uqbTxq/NykKwgu/AV/rQrkJgIOLLKlpEGb+b2PTrmTiKR8/J1bnsFfhhM0VMPqVg3VhG5jtjEMpqRoxDfRocnshB7xlu5hXu2Gi566V48XOu79PiOSB6wW69APoINDCwS1nP6sh+nD/129nvFPceFAUpHlADr/zh2pwBAdk39JEV6CR4Fj7M47EYv/DP/83gjSrusnH1qqL8hmXujwIM/2/MBRZWxifZ6QvwYzdP42N9VAgMBAAECggEAOOi/xw0xrMAgAi9xyMNogD+2FaTnfPpVnSjawWbPjiR1/Q6DWdxElwRIs1zGoOtPubkUAKngPFqGrIusXMb3fBcJVPZgu3Rkrah/rGEwhcLpJPclxQwWEUN1FAGyRWAmCQGNpqMaAyC6dxJEDXXwESkIbYZBcDqSCctr30+obqaQvUsElE64FYeqO47EtOUiYt2hXFt95Mezn710hp7x5xj1RUgE7+YXos5Lf/PYGHfj7RxJR/HcVB27ubs/n8lDD/2E1pSwBVanpCXMfuFtKF48mdZz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otzLKQ6E0ZzPBEiWPVJlg0FoVlvy+F8qee8lc3JpaqawEy8iYXyWunu/uHWF/mk4aCw+7ECxe4cDGSAxJggMGPCyLORpphFDFxpvBenLgWWNJ0eN8h09ziiYhYHzEIRUqjvWpoUIDTLgPF40CZbvz/J43IT9+fZM2fNGsuwAY5+vJ6drhsK8E7HXxuuhdGQQC+sz5BTFoBr9zSinBEBpddZ4rX6BU28fd6iJa9hJ00QQ7NEjx2jRAf5poEcIfB8QIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId("2021005188622285");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}
